/**
 * Created by kenz on 2018/5/9.
 */
//papers 结构
// const papersData = [{
//   paperName: '客观试卷1',
//   id: 1,
//   subject: [{
//     id: 2,
//     type: 1, // 题型
//     question: '', //问题
//     answer: ['', '', '', ''], // 可选答案, 单选多选的答案 数组 length 必须大于 2， 判断题 ['错误', '正确'] 直接用索引 0 1,
//     trueAnswer: null, // 标准答案，老师填入或选择的, 数据格式根据type处理，一般数组性质的答案，用 逗号 分割成 字符串
//     content: '', // 解析内容
//     score: 1,  //本题得分
//     detailedScore: [], // 详细分值，填空/综合的时候需要配置
//     children: [{
//       id: 3,
//       type: 1, // 题型
//       question: '', //问题
//       answer: [], // 答案
//     }],
//     status: true
//   }]
// }]

const subjectData = JSON.stringify({
  id: 0,
  type: 1, // 题型
  question: '', //问题
  answer: ['', '', '', ''], // 可选答案, 单选多选的答案 数组 length 必须大于 2， 判断题 ['错误', '正确'] 直接用索引 0 1,
  trueAnswer: null, // 标准答案，老师填入或选择的, 数据格式根据type处理，一般数组性质的答案，用 逗号 分割成 字符串
  content: '', // 解析内容
  score: 1,  //本题得分
  detailedScore: [], // 详细分值，填空/综合的时候需要配置
  children: [{
    id: 0,
    type: 1, // 题型
    question: '', //问题
    answer: [], // 答案
  }],
  status: false
})

const initAnswer = ['', ['', '', '', ''], ['', '', '', ''], ['错误', '正确'], [], '', '', '']
const initTrueAnswer = ['', '', [], '', [], '', '', '']
let letter = []
for(let i=0; i<10; i++) {
  letter.push(String.fromCharCode(65 + i))
}

const state = {
  papers: [],
  status: true,
}


const getters = {
  getPaper: state => {
    return state.papers
  },
  getPaperStatus: state => {
    return state.status
  }
}


const mutations = {
  setPaper: (state, paper) => {
    if (paper.length) {
      state.papers = paper
    } else {
      state.papers = [{
        paperName: '',
        id: 0,
        subject: [JSON.parse(subjectData)]
      }]
    }
    state.status = false
  },
  // 添加一个题目到试卷中
  addPaper: (state, idx) => {
    //添加时，必须当前没有未完成的题目
    if (state.status) {
      state.papers[idx].subject.unshift(JSON.parse(subjectData))
      state.status = false
    }
  },
  // 删除试卷的某一个题目 idx 表示试卷索引，  index表示题目索引
  delPaper: (state, filed) => {
    const { idx, index } = filed
    state.papers[idx].subject.splice(index, 1)
    state.status = true
  },

  //改变题目类型 idx 表示试卷索引， index 表示题目索引
  changePaperType(state, filed) {
    const { idx, index, type } = filed
    let subjectItem = state.papers[idx].subject[index]
    state.papers[idx].subject[index] = Object.assign({}, subjectItem, {
      type,
      trueAnswer: JSON.parse(JSON.stringify(initTrueAnswer[type])),
      answer: JSON.parse(JSON.stringify(initAnswer[type])),
      detailedScore: [],
      status: false
    })
  },

  //改变分值 idx 表示试卷索引， index 表示题目索引
  changePaperScore(state, filed) {
    const { index, idx, score } = filed
    let subjectItem = state.papers[idx].subject[index]
    subjectItem.score = score
    subjectItem.status = false
  },

  //设置答案分值
  setPaperDetailedScore(state, filed) {
    const { index, idx, i, score } = filed
    state.papers[idx].subject[index].detailedScore[i] = score
  },

  //设置题目 idx 表示试卷索引， index 表示题目索引
  setPaperQuestion(state, filed) {
    const { idx, index, question } = filed
    let subjectItem = state.papers[idx].subject[index]
    subjectItem.question = question
    subjectItem.status = false
    const { type } = subjectItem
    if (type == 4) {
      const len = question.split('__').length - 1
      subjectItem.trueAnswer = new Array(len).fill('')
      subjectItem.detailedScore = new Array(len).fill(0)
    }
  },

  //添加一个答案选项
  addPaperAnswer(state, filed) {
    const { idx, index } = filed
    state.papers[idx].subject[index].answer.push('')
  },

  //设置答案 idx 表示试卷索引， index 表示题目索引 i 表示答案的索引
  setPaperAnswer(state, filed) {
    const { idx, index, i, answer } = filed
    state.papers[idx].subject[index].answer[i] = answer
  },

  //设置答案 idx 表示试卷索引， index 表示题目索引 i 表示答案的索引
  setPaperTrueAnswer(state, filed) {
    let { idx, index, i, answer } = filed
    answer = answer || ''
    let subjectItem = state.papers[idx].subject[index]
    let { type, trueAnswer } = subjectItem
    switch (type) {
      case 1:
        subjectItem.trueAnswer = letter[i]
        break;
      case 2:
        let j = trueAnswer.findIndex(pet => pet === letter[i])
        if (j === -1) {
          subjectItem.trueAnswer.push(letter[i])
          subjectItem.trueAnswer.sort()
        } else {
          subjectItem.trueAnswer.splice(j, 1)
        }
        break;
      case 3:
        subjectItem.trueAnswer = i
        break;
      case 4:
        subjectItem.trueAnswer[i] = answer
        break;
    }

  },

  //删除答案 idx 表示试卷索引， index 表示题目索引 i 表示答案的索引
  delPaperAnswer(state, filed) {
    const { idx, index, i } = filed
    //如果删除时，这个选项被选为正确答案，需要清空下正确答案
    let subjectItem = state.papers[idx].subject[index]
    const { type, trueAnswer } = subjectItem
    switch (type) {
      case 1:
        if(letter[i] === trueAnswer && type == 1) {
          subjectItem.trueAnswer = ''
        }
        break;
      case 2:
        const j = trueAnswer.findIndex(pet => pet === letter[i])
        if (j > -1) {
          subjectItem.trueAnswer.splice(j, 1)
        }
        if (trueAnswer.length) {
          let newTrueAnswer = []
          // 截取当前删除后面的所有字母集合
          let newLetter = letter.slice(i + 1)
          // 对选中的答案在遍历一次
          trueAnswer.map(item => {
            //找出答案在新字母数组中的位置，
            const n = newLetter.findIndex(pet => pet === item)
            // 找不到表示删除的那个选项在后面，前面的选项直接push即可，不用对应的 - 1
            if(n == -1) {
              newTrueAnswer.push(item)
            } else {
              let u = letter.findIndex(pet => pet === newLetter[n])
              newTrueAnswer.push(letter[u - 1])
            }
          })
          subjectItem.trueAnswer = newTrueAnswer
        }
        break;
    }
    subjectItem.answer.splice(i, 1)
  },

  //设置解析内容 idx 表示试卷索引， index 表示题目索引
  setPaperContent(state, filed) {
    const { index, idx, content } = filed
    state.papers[idx].subject[index].content = content
  },

  //对数据进行上移下移
  setPaperDirection(state, filed) {
    const { idx, index, direction } = filed
    let subjectItem = state.papers[idx].subject
    const newSubject = JSON.parse(JSON.stringify(subjectItem[index]))
    const i = direction === 'up'? index - 1: index + 1
    subjectItem.splice(index, 1)
    subjectItem.splice(i, 0, newSubject)
  },

  setStatus(state, filed) {
    const { idx, index, status } = filed
    state.status = status
    state.papers[idx].subject[index].status = status
  }
}

export default {
  state,
  getters,
  mutations
}

